﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.DataObject;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess.BusinessObject
{
    public class BPost:Common.CDBase
    {
        BCategory_Post _BCategory_Post = new BCategory_Post();
        /// <summary>
        /// Thêm bài viết
        /// </summary>
        /// <param name="_OPost"></param>
        /// <returns></returns>
        public bool Add(OPost _OPost,List<int> lstCatID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@Title",SqlDbType.NVarChar)
                                      , new SqlParameter("@Slug",SqlDbType.NVarChar)
                                      , new SqlParameter("@Excerpt",SqlDbType.NVarChar)
                                      , new SqlParameter("@Content",SqlDbType.NVarChar)
                                      , new SqlParameter("@FeaturedImage",SqlDbType.NVarChar)
                                      , new SqlParameter("@CreateDate",SqlDbType.DateTime)
                                      , new SqlParameter("@Status",SqlDbType.NVarChar)
                                      , new SqlParameter("@UserCreate",SqlDbType.Int)
                                      , new SqlParameter("@LangID",SqlDbType.VarChar)
                                      , new SqlParameter("@Type",SqlDbType.VarChar)};
            sqlParam[0].Value = _OPost.Title;
            sqlParam[1].Value = _OPost.Slug;
            sqlParam[2].Value = _OPost.Excerpt;
            sqlParam[3].Value = _OPost.Content;
            sqlParam[4].Value = _OPost.FeaturedImage;
            sqlParam[5].Value = _OPost.CreateDate;
            sqlParam[6].Value = _OPost.Status;
            sqlParam[7].Value = _OPost.UserCreate;
            sqlParam[8].Value = _OPost.LangID;
            sqlParam[9].Value = _OPost.Type;
            if (RunProcedure("sp_tblPost_add", sqlParam))
            {
                int IDPost = this.GetIDLastRecord();
                if (IDPost > 0)
                {
                    if (lstCatID != null)
                    {
                        foreach (int IDCat in lstCatID)
                        {
                            _BCategory_Post.Add(IDCat, IDPost);
                        }                        
                    }
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// Lấy về danh sách đối tượng OPost theo PostID, nếu PostID=0 lấy về 50 bài viết mới nhất
        /// </summary>
        /// <param name="PostID"></param>
        /// <returns></returns>
        public IList<OPost> Get(int PostID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@PostID",SqlDbType.Int)};
            sqlParam[0].Value = PostID;
            return Common.Common.ConvertTo<OPost>(RunProcedureGet("sp_tblPost_get", sqlParam));
        }
        /// <summary>
        /// Lấy về danh sách bài viết theo category
        /// </summary>
        /// <param name="CatID"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public IList<OPost> Get(int CatID,int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@CatID", SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
            sqlParam[0].Value = CatID;
            sqlParam[1].Value = PageSize;
            return Common.Common.ConvertTo<OPost>(RunProcedureGet("sp_tblPost_get", sqlParam));
        }
        public IList<OPost> Get(string Title,string Status, string LandID,string Type, int CatID, string Order, string OrderBy, int PageIndex, int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@Title", SqlDbType.NVarChar),
                                        new SqlParameter("@Content",SqlDbType.NVarChar),
                                        new SqlParameter("@Status",SqlDbType.NVarChar),
                                        new SqlParameter("@LangID",SqlDbType.VarChar),
                                        new SqlParameter("@Type",SqlDbType.VarChar),
                                        new SqlParameter("@CatID",SqlDbType.Int),
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
            sqlParam[0].Value = Title;
            sqlParam[1].Value = Title;
            sqlParam[2].Value = Status;
            sqlParam[3].Value = LandID;
            sqlParam[4].Value = Type;
            sqlParam[5].Value = CatID;
            sqlParam[6].Value = Order;
            sqlParam[7].Value = OrderBy;
            sqlParam[8].Value = PageIndex;
            sqlParam[9].Value = PageSize;
            return Common.Common.ConvertTo<OPost>(RunProcedureGet("sp_tblPost_get", sqlParam));
        }
        /// <summary>
        /// Lấy về danh sách đối tượng OPost
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="FromCreateDate"></param>
        /// <param name="ToCreateDate"></param>
        /// <param name="Status"></param>
        /// <param name="LandID"></param>
        /// <param name="CatID"></param>
        /// <param name="Order"></param>
        /// <param name="OrderBy"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public IList<OPost> Get(string Title,DateTime FromCreateDate,DateTime ToCreateDate,string Status,string LandID,int CatID,string Order,string OrderBy,int PageIndex,int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@Title", SqlDbType.NVarChar),
                                        new SqlParameter("@Content",SqlDbType.NVarChar),
                                        new SqlParameter("@FromCreateDate",SqlDbType.DateTime),
                                        new SqlParameter("@ToCreateDate",SqlDbType.DateTime),
                                        new SqlParameter("@Status",SqlDbType.NVarChar),
                                        new SqlParameter("@LangID",SqlDbType.VarChar),
                                        new SqlParameter("@CatID",SqlDbType.Int),
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
            sqlParam[0].Value = Title;
            sqlParam[1].Value = Title;
            sqlParam[2].Value = FromCreateDate;
            sqlParam[3].Value = ToCreateDate;
            sqlParam[4].Value = Status;
            sqlParam[5].Value = LandID;
            sqlParam[6].Value = CatID;
            sqlParam[7].Value = Order;
            sqlParam[8].Value = OrderBy;
            sqlParam[9].Value = PageIndex;
            sqlParam[10].Value = PageSize;
            return Common.Common.ConvertTo<OPost>(RunProcedureGet("sp_tblPost_get", sqlParam));
        }
        public IList<OPost> Search(string Title, string Order, string OrderBy, Nullable<int> PageIndex, Nullable<int> PageSize)
        {
            if (PageIndex != null && PageSize != null)
            {
                SqlParameter[] sqlParam = { new SqlParameter("@Title", SqlDbType.NVarChar),
                                        new SqlParameter("@Content",SqlDbType.NVarChar),                                        
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
                sqlParam[0].Value = Title;
                sqlParam[1].Value = Title;
                sqlParam[2].Value = Order;
                sqlParam[3].Value = OrderBy;
                sqlParam[4].Value = PageIndex;
                sqlParam[5].Value = PageSize;
                return Common.Common.ConvertTo<OPost>(RunProcedureGet("sp_tblPost_get", sqlParam));
            }
            else {
                SqlParameter[] sqlParam = { new SqlParameter("@Title", SqlDbType.NVarChar),
                                        new SqlParameter("@Content",SqlDbType.NVarChar),                                        
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),};
                sqlParam[0].Value = Title;
                sqlParam[1].Value = Title;
                sqlParam[2].Value = Order;
                sqlParam[3].Value = OrderBy;
                return Common.Common.ConvertTo<OPost>(RunProcedureGet("sp_tblPost_get", sqlParam));
            }
        }
        /// <summary>
        /// Cập nhật bài viết
        /// </summary>
        /// <param name="_OPost"></param>
        /// <returns></returns>
        public bool Update(OPost _OPost,List<int> lstCatID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@PostID",SqlDbType.Int)
                                      , new SqlParameter("@Title",SqlDbType.NVarChar)
                                      , new SqlParameter("@Slug",SqlDbType.NVarChar)
                                      , new SqlParameter("@Excerpt",SqlDbType.NVarChar)
                                      , new SqlParameter("@Content",SqlDbType.NVarChar)
                                      , new SqlParameter("@FeaturedImage",SqlDbType.NVarChar)
                                      , new SqlParameter("@CreateDate",SqlDbType.DateTime)
                                      , new SqlParameter("@Status",SqlDbType.NVarChar)
                                      , new SqlParameter("@LangID",SqlDbType.VarChar)};
            sqlParam[0].Value = _OPost.PostID;
            sqlParam[1].Value = _OPost.Title;
            sqlParam[2].Value = _OPost.Slug;
            sqlParam[3].Value = _OPost.Excerpt;
            sqlParam[4].Value = _OPost.Content;
            sqlParam[5].Value = _OPost.FeaturedImage;
            sqlParam[6].Value = _OPost.CreateDate;
            sqlParam[7].Value = _OPost.Status;            
            sqlParam[8].Value = _OPost.LangID;
            if (RunProcedure("sp_tblPost_update", sqlParam))
            {
                _BCategory_Post.Delete(_OPost.PostID);
                if (lstCatID != null)
                {
                    foreach (int IDCat in lstCatID)
                    {
                        _BCategory_Post.Add(IDCat, _OPost.PostID);
                    }                    
                }
                return true;
            }
            return false;
        }
        /// <summary>
        /// Lấy về PostID của bài viết vừa thêm vào
        /// </summary>
        /// <returns></returns>
        public int GetIDLastRecord()
        {
            DataTable dtb = RunProcedureGet("sp_tblPost_getidlastrecord");
            return int.Parse(dtb.Rows[0]["PostID"].ToString());
        }
        /// <summary>
        /// Xóa bài viết
        /// </summary>
        /// <param name="PostID"></param>
        /// <returns></returns>
        public bool Delete(int PostID)
        {
            _BCategory_Post.Delete(PostID);
            SqlParameter[] sqlParam = { new SqlParameter("@PostID",SqlDbType.Int)};
            sqlParam[0].Value = PostID;
            return RunProcedure("sp_tblPost_delete", sqlParam);            
        }
    }
}
